package apis

import (
	"github.com/gin-gonic/gin"
	"fmt"
	"hulk-api/models"
	"net/http"
)

func HandleUserManager(c *gin.Context) {
	var u models.User
	//c.BindJSON(&u)   //TODO：ROLE 鉴权

	//account := c.Request.FormValue("username")
	//passWord := c.Request.FormValue("password")
	//u := models.User{Account:account,Password:passWord}

	users,err := u.GetUsers()
	if err != nil {
		fmt.Println("error is",err)
	}
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"users": users,
	})

}

func DeleteUser(c *gin.Context) {
	var u models.User
	c.Bind(&u)   //TODO：ROLE 鉴权
	//account := c.Request.FormValue("account")
	fmt.Println("u is " , u.Account)
	//fmt.Println("account is " , account)



	err := u.DeleteUser()
	if err != nil {
		fmt.Println("error is",err)
	}
	c.JSON(http.StatusOK,gin.H{
		"code": http.StatusOK,
	})

}

func AddUser(c *gin.Context) {
	var u models.User
	c.Bind(&u)   //TODO：ROLE 鉴权
	//account := c.Request.FormValue("account")
	fmt.Println("u is " , u.Account)
	//fmt.Println("account is " , account)

	err := u.AddUser()
	if err != nil {
		fmt.Println("error is",err)
	}
	c.JSON(http.StatusOK,gin.H{
		"code": http.StatusOK,
	})

}


func ResetPassword(c *gin.Context) {
	fmt.Println("debug 01")
	var u models.User
	c.BindJSON(&u)
	//account := c.Request.FormValue("account")
	fmt.Println("u is " , u.Account)
	fmt.Println("u is " , u.Password)
	//fmt.Println("account is " , account)



	err := u.ResetPassword()
	if err != nil {
		fmt.Println("error is",err)
	}
	c.JSON(http.StatusOK,gin.H{
		"code": http.StatusOK,
	})

}

